﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;
using System.Globalization;
using System.Threading;
using System.Media;
using System.Runtime.InteropServices;

namespace ThousandFunClient
{
    public partial class Fm_ProNo_config : Form
    {
        public Fm_ProNo_config()
        {
            InitializeComponent();
        }

        private string prono = System.Configuration.ConfigurationSettings.AppSettings["PortCSendProgramNo"];

        private void btn_changekeyboard_Click(object sender, EventArgs e)
        {
            this.txt_password.Focus();
            System.Diagnostics.Process.Start("osk.exe"); 
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            if (Constants.MainPassword == txt_password.Text.Trim())
            {
                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                cfa.AppSettings.Settings["PortCSendProgramNo"].Value = btn_ten.Text.Trim()+btn_one.Text.Trim();
                cfa.Save(ConfigurationSaveMode.Modified);

                ConfigurationManager.RefreshSection("appSettings");
                this.Hide();
            }
            else
            {
                MessageBox.Show(this, "密码错误");
                txt_password.Focus();
            }

            txt_password.Text = "";
            

        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            txt_password.Text = "";
            this.Hide();
        }

        private void btn_ten_Click(object sender, EventArgs e)
        {
            if (btn_ten.Text.Trim() == "0")
            {
                btn_ten.Text = "1";
                if (int.Parse(btn_one.Text.Trim()) >= 6)
                {
                    btn_one.Text = "1";
                }
            }
            else
            {
                btn_ten.Text = "0";
            }

        }

        private void btn_one_Click(object sender, EventArgs e)
        {
            string cc = btn_one.Text.Trim();
            int icc = int.Parse(cc);
            icc = (icc + 1) % 10;
            if (0 == icc)
            {
                icc = 1;
            }
            if (icc >= 6 && "1" == btn_ten.Text)
            {
                icc = 1;
            }


            btn_one.Text = icc.ToString();
        }

        private void Fm_ProNo_config_Load(object sender, EventArgs e)
        {
            string portCSendProgramNo = System.Configuration.ConfigurationSettings.AppSettings["PortCSendProgramNo"];
            try
            {
                btn_ten.Text = portCSendProgramNo.Substring(0, 1);
                btn_one.Text = portCSendProgramNo.Substring(1, 1);
            }
            catch(Exception ex)
            {
                btn_ten.Text = "0";
                btn_one.Text = "0";
            }
        }
    }
}
